#include "stdio.h"
#include <unistd.h>

#include <unistd.h>
#include <ios>
#include <iostream>
#include <fstream>
#include <string>
#include <QSharedPointer>
#include "gc.h"
#include "gc_cpp.h"
#include "windows.h"
class TClass : public gc_cleanup  {
public:
    int x;
    int *y;
    QString *foo;
    TClass(int i);
    ~TClass();

};



void process_mem_usage(double& vm_usage, double& resident_set)
{
  /* using std::ios_base;
   using std::ifstream;
   using std::string;

   vm_usage     = 0.0;
   resident_set = 0.0;

   // 'file' stat seems to give the most reliable results
   //
   ifstream stat_stream("/proc/self/stat",ios_base::in);

   // dummy vars for leading entries in stat that we don't care about
   //
   string pid, comm, state, ppid, pgrp, session, tty_nr;
   string tpgid, flags, minflt, cminflt, majflt, cmajflt;
   string utime, stime, cutime, cstime, priority, nice;
   string O, itrealvalue, starttime;

   // the two fields we want
   //
   unsigned long vsize;
   long rss;

   stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
               >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
               >> utime >> stime >> cutime >> cstime >> priority >> nice
               >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest

   long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
   vm_usage     = vsize / 1024.0;
   resident_set = rss * page_size_kb;*/
}


TClass::TClass(int i)
{
    x = i;
    double vm, rss;
    vm = (double)GC_get_heap_size();
    foo = new (GC) QString("foo");
    process_mem_usage(vm, rss);
    y = new (GC) int[10000];

    printf("Constructed %i %d %d \n", x, vm, rss);
}
TClass::~TClass()
{
    delete foo;
    printf("Destroy %i \n", x);
}
int foo(int x)
{
    TClass *c = new  (GC)TClass(x);
}

int main(int argc, char *argv[])
{
    GC_INIT();
    for (int x = 0; x < 1000000; x ++) {
        foo(x);
    }
    Sleep(10000);
    return 0;
}
